import java.io.*;

public class 区间最大值_线段树 {
    static int N = 100010;
    static Node1[] tree = new Node1[4*N];
    static int[] arr = new int[N];
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] str = in.readLine().split( " ");
        int n = Integer.parseInt(str[0]);int m = Integer.parseInt(str[1]);
        String[] str2 = in.readLine().split(" ");
        for (int i=1;i<=n;i++) arr[i] = Integer.parseInt(str2[i-1]);

        build(1,1,n);
        while(m-- >0) {
            String[] str3 = in.readLine().split(" ");
            int a = Integer.parseInt(str3[0]);int b = Integer.parseInt(str3[1]);
            out.write(query(1, a, b)+"\n");
        }
        out.flush();
    }

    private static void pushUp(int u) {
        tree[u].max = Math.max(tree[u<<1].max,tree[u<<1|1].max);
    }
    private static void build(int u,int l,int r) {
        //叶子结点
        if(l==r) tree[u] = new Node1(l,r,arr[r]);
        else {
            tree[u] = new Node1(l,r,0);
            int mid = tree[u].l+tree[u].r >>1;
            build(u<<1,l,mid);build(u<<1|1,mid+1,r);
            pushUp(u);
        }
    }
    private static int query(int u,int l,int r) {
        if(l<=tree[u].l && tree[u].r<=r) return tree[u].max;
        int mid = tree[u].l+tree[u].r >> 1;
        int max = -10000000;
        if(mid>=l) max = Math.max(max,query(u<<1,l,r));
        if(mid<r) max = Math.max(max,query(u<<1|1,l,r));
        return max;
    }

}
class Node1 {
    int l;
    int r;
    int max;

    public Node1(int l, int r, int max) {
        this.l = l;
        this.r = r;
        this.max = max;
    }
}
